package ch.il07.converter.presentation.beans;

import java.util.ArrayList;

import ch.il07.converter.business.Converter;
import ch.il07.converter.business.MeasurandAction;
import ch.il07.converter.business.ServiceFactoryAdapter;
import ch.il07.converter.persistence.entities.Measurand;
import ch.il07.converter.persistence.entities.Unit;
import ch.il07.converter.persistence.exceptions.NoSuchMeasurandException;
import ch.il07.converter.persistence.exceptions.NoSuchUnitException;
import ch.il07.converter.persistence.exceptions.NoSuchUserException;
import ch.il07.converter.presentation.ErrorHandler;
import ch.il07.converter.presentation.Helper;

public class IndexBean {

	private String measurand;
	
	private String fromUnit;
	private String toUnit;

	private Double amount = 1d;
	
	private Double result;

	/* User chooses from this list */
	private ArrayList<String> unitList;
	private ArrayList<String> measurandList;
	
	private String username;
	
	
	public IndexBean() {
		username = Helper.getUserName();
		setMeasurand(null);
	}
	
	public String updateList() {
		fillMeasurandList();
		fillUnitList();
		return "showIndex";
	}
	
	public String changeMeasurand() {
//		System.out.println("Measurand: " + getMeasurand());
//		System.out.println("Amount:    " + getAmount());
//		System.out.println("From:      " + getFromUnit());
//		System.out.println("To:        " + getToUnit());
		return "success";
	}

	public String process() {
		username = Helper.getUserName();
		ErrorHandler errorhandler = Helper.getErrorHandler();
		
//		System.out.println("username:  " + username);
//		System.out.println("Measurand: " + getMeasurand());
//		System.out.println("Amount:    " + getAmount());
//		System.out.println("From:      " + getFromUnit());
//		System.out.println("To:        " + getToUnit());
		
		Converter c = ServiceFactoryAdapter.getConverterInstance();
		try {
			if(null == c) {
				errorhandler.add("Beim Berechnen ist ein Fehler aufgetreten. =(");
				return "failure";
			}
			result = c.calculate(username, measurand, fromUnit, toUnit, amount);
		} catch (NoSuchMeasurandException e) {
			errorhandler.add("Die Messgrösse wurde nicht gefunden.");
		} catch (NoSuchUserException e) {
			errorhandler.add("Bitte benutzen Sie das Loginformular, um sich einzuloggen.");
		} catch (NoSuchUnitException e) {
			errorhandler.add("Die Einheit wurde nicht gefunden.");
		} catch (Exception e) {
			errorhandler.add("Hoppla.. Ein unbekannter Fehler ist aufgetreten :-(");
		}
	
		if(!errorhandler.hasErrors())
			errorhandler.add("success", "Es wurde erfolgreich umgerechnet.");
		
		return "success";
	}
	
	private void fillMeasurandList() {
		if("".equals(username))
			username = null;
		
		measurandList = new ArrayList<String>();
		
		MeasurandAction ma = ServiceFactoryAdapter.getMeasurandActionInstance();
		for(Measurand m : ma.getAllPublicMeasurands()) {
			if(null == measurand) measurand = m.getName();
			measurandList.add(m.getName());
		}
		
		if(username != null) {
			for(Measurand m : ServiceFactoryAdapter.getMeasurandActionInstance().getAllPrivateMeasurands(username)) {
				if(null == measurand) measurand = m.getName();
				measurandList.add(m.getName());
			}
		}
	}
	
	private void fillUnitList() {
		if("".equals(username))
			username = null;
		
		unitList = new ArrayList<String>();
		if(null == measurand) return;
		
		for(Unit u : ServiceFactoryAdapter.getUnitActionInstance().getPublicUnitsMeasurand(measurand)) {
			unitList.add(u.getName());
		}
		
		if(username != null) {
			for(Unit u : ServiceFactoryAdapter.getUnitActionInstance().getPrivateUnitsMeasurand(measurand, username)) {
				unitList.add(u.getName());
			}
		}
	}

	/**
	 * @return the measurand
	 */
	public String getMeasurand() {
		return measurand;
	}

	/**
	 * @param measurand the measurand to set
	 */
	public void setMeasurand(String measurand) {
		if(null == this.measurand || !measurand.equals(this.measurand)) {
			Helper.getErrorHandler().add("info", "Die Messgrösse wurde geändert.");
			fromUnit = null;
			toUnit   = null;
			this.measurand = measurand;
			fillMeasurandList();
			fillUnitList();
		}
	}

	/**
	 * @return the fromUnit
	 */
	public String getFromUnit() {
		return fromUnit;
	}

	/**
	 * @param fromUnit the fromUnit to set
	 */
	public void setFromUnit(String fromUnit) {
		this.fromUnit = fromUnit;
	}

	/**
	 * @return the toUnit
	 */
	public String getToUnit() {
		return toUnit;
	}

	/**
	 * @param toUnit the toUnit to set
	 */
	public void setToUnit(String toUnit) {
		this.toUnit = toUnit;
	}

	/**
	 * @return the amount
	 */
	public Double getAmount() {
		return amount;
	}

	/**
	 * @param amount the amount to set
	 */
	public void setAmount(Double amount) {
		this.amount = amount;
	}

	/**
	 * @return the result
	 */
	public Double getResult() {
		return result;
	}

	/**
	 * @param result the result to set
	 */
	public void setResult(Double result) {
		this.result = result;
	}

	public ArrayList<String> getUnitList() {
		return unitList;
	}

	public void setUnitList(ArrayList<String> unitList) {
		this.unitList = unitList;
	}

	public ArrayList<String> getMeasurandList() {
		return measurandList;
	}

	public void setMeasurandList(ArrayList<String> measurandList) {
		this.measurandList = measurandList;
	}
}
